package com.infomanage.service.tech;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.TechRescueLogDAO;
import com.infomanage.dao.TechStuffConsumeDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.TechStuffconsume;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.TechStuffconsumeItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class TechStuffConsumeService {

	public static TechStuffconsumeItem loadStuffConsume(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId= request.getParameter("itemId");
			TechStuffConsumeDAO dao = new TechStuffConsumeDAO();
			if(StringUtils.isEmpty(itemId)) return null;
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<TechStuffconsumeItem> loadStuffConsumeList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			TechStuffConsumeDAO dao = new TechStuffConsumeDAO();
			List<TechStuffconsumeItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject saveStuffConsume(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			TechStuffconsume bean = new TechStuffconsume();
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setDescription(request.getParameter("description"));
			bean.setName(request.getParameter("name"));
			bean.setNum(request.getParameter("num"));
			bean.setPersonId(Integer.parseInt(request.getParameter("personId")));
			bean.setPurpose(request.getParameter("purpose"));
			bean.setType(request.getParameter("type"));
			TechStuffConsumeDAO dao = new TechStuffConsumeDAO();
			dao.add(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject removeStuffConsume(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String studyId = request.getParameter("itemId");
			TechStuffConsumeDAO dao = new TechStuffConsumeDAO();
			dao.remove(TechStuffconsume.class, Integer.parseInt(studyId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject removeBatchStuffConsume(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			TechStuffConsumeDAO dao = new TechStuffConsumeDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(TechStuffconsume.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject modifyStuffConsume(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int planId = Integer.parseInt(request.getParameter("itemId"));
			TechStuffConsumeDAO dao = new TechStuffConsumeDAO();
			TechStuffconsume bean = dao.find(TechStuffconsume.class, planId);
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setDescription(request.getParameter("description"));
			bean.setName(request.getParameter("name"));
			bean.setNum(request.getParameter("num"));
			bean.setPersonId(Integer.parseInt(request.getParameter("personId")));
			bean.setPurpose(request.getParameter("purpose"));
			bean.setType(request.getParameter("type"));
			dao.modify(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static List<TechStuffconsumeItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			TechStuffConsumeDAO dao = new TechStuffConsumeDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<TechStuffconsumeItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			TechStuffConsumeDAO dao = new TechStuffConsumeDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String deptId = request.getParameter("deptId");
		String name = request.getParameter("name");
		SearchBean searchBean = new SearchBean();
		if(!StringUtil.isEmpty(deptId)){
			FieldBean field = new FieldBean("deptId", Integer.parseInt(deptId));
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(name)){
			FieldBean field = new FieldBean("name", name);
			field.setLike(true);
			searchBean.add(field);
		}
		
		//设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if(user != null){
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
